package com.zetyun.tiger.app.opac;

import com.zetyun.rt.meta.annotation.ActionMeta;
import com.zetyun.rt.sdk.action.MapAction;
import com.zetyun.rt.sdk.model.RtEvent;

import static com.zetyun.tiger.app.opac.FormatConstant.*;

@ActionMeta(
        id = "tiger.opac-format",
        tags = {"opac"},
        category = "map",
        name = "Opac-Format",
        description = "开销户格式化算子",
        parameterClass = OpacParameter.class,
        status = ActionMeta.Status.PUBLIC
)
public class OpacFormatAction extends MapAction {

    private String cb_corporation = "";                   // 法人行号
    private String cb_protal_opun_cod = "";                   // 机构编号（门户）
    private String cb_pa_opun_cod = "";                   // 业务归属机构号
    private String cb_conntr_no = "";                   // 客户经理编号
    private String cb_conntr_name = "";                   // 客户经理姓名      -
    private String cb_cust_no = "";                   // 客户编号
    private String cb_act_cust_name = "";                   // 客户名称
    private String cb_pc_sign = "";                   // 客户对公对私标志
    private String cb_acct_no = "";                   // 内部账号
    private String cb_out_acct_no = "";                   // 外部账号
    private String cb_card_no = "";                   // 卡号
    private String cb_vip_acct_flg = "";                   // 重要账户标志
    private String cb_pub_dep_flg = "";                   // 重要账户标志

    private String cb_opac_typ = "";                   // 开销户标识  (开户)
    private String cb_opac_typ_cn = "";                   // 开销户中文  (开户)
    private String cb_opac_amt = "";                   // 开户金额
    private String cb_opac_instn_no = "";                   // 开户机构号
    private String cb_opac_tlr_no = "";                   // 开户柜员号
    private String cb_opac_channel_flag = "";                   // 开户渠道
    private String cb_opac_dt = "";                   // 开户日期
    private String cb_cacct_instn_no = "";                   // 销户机构号
    private String cb_cacct_dt = "";                   // 销户日期
    private String cb_pdp_code = "";                   // 产品代码
    private String cb_event_date = "";                   // 事件日期
    private String cb_event_hour = "";                   // 所属小时
    private String cb_event_minu = "";                   // 所属分钟
    private String cb_event_time = "";                   // 事件时间
    private long cb_proc_time ;                         // 处理时间

    @Override
    public RtEvent apply(RtEvent in) throws Exception {

        // 根据定存活存数据分别处理
        String topic = in.getHeader("topic", String.class);

        String flag = "SA";
        if (topic.contains("TDACNACN")) {
            flag = "TD";
            cb_cacct_dt           = in.getField(RT_EVENT_DATE, String.class);
        } else {
            cb_cacct_dt           = in.getField("SA_CACCT_DT", String.class);
        }

        String tdacn_key =       in.getField(flag+"ACN_KEY", String.class);
        cb_corporation =         tdacn_key.substring(0, 3);
        cb_conntr_no =           in.getField(flag+"_CONNTR_NO", String.class);
        cb_cust_no =             in.getField(flag+"_CUST_NO", String.class);
        cb_act_cust_name =       in.getField(flag+"_ACT_CUST_NAME", String.class);
        String pcFlag =          tdacn_key.substring(12, 14);
        cb_pc_sign =             (pcFlag.equals("27")) ? "P" : "C";
        cb_acct_no            = tdacn_key;
//            cb_out_acct_no        = in.getField("CB_OUT_ACCT_NO", String.class);
        cb_card_no            = in.getField(flag+"_CARD_NO", String.class);
        cb_vip_acct_flg       = in.getField(flag+"_VIP_ACCT_FLG", String.class);

        if (cb_conntr_no.isEmpty()) {
            cb_pub_dep_flg = "Y";
        } else {
            cb_pub_dep_flg = "N";
        }


        cb_opac_amt           = in.getField(flag+"_OPAC_AMT", String.class);
        cb_opac_instn_no      = in.getField(flag+"_OPAC_INSTN_NO", String.class);
        cb_opac_tlr_no        = in.getField(flag+"_OPAC_TLR_NO", String.class);
        cb_opac_channel_flag  = in.getField(flag+"_OPAC_CHANNEL_FLAG", String.class);
        cb_opac_dt            = in.getField(flag+"_OPAC_DT", String.class);
        cb_cacct_instn_no     = in.getField(flag+"_CACCT_INSTN_NO", String.class);

        cb_pdp_code           = in.getField(flag+"_PDP_CODE", String.class);
        cb_event_date         = in.getField(RT_EVENT_DATE, String.class);
        cb_event_hour         = in.getField(RT_EVENT_HOUR, String.class);
        cb_event_minu         = in.getField(RT_EVENT_MINU, String.class);
        cb_event_time         = in.getField(RT_EVENT_TIME, String.class);

        cb_proc_time          = System.currentTimeMillis();

        if (in.getField(flag+"_CACCT_INSTN_NO", String.class).isEmpty()) {
            cb_opac_typ           = "01";
            cb_opac_typ_cn        = "开户";
            // 判断开户日期是否与 RT_EVENT_DATE 相等，相等即为今日开户，不相等即为往日开户，过滤掉数据
            if (!cb_opac_dt.equals(cb_event_date)) {
                return null;
            }
        } else {
            cb_opac_typ           = "02";
            cb_opac_typ_cn        = "销户";
        }

        RtEvent event = new RtEvent();

        event.setValue(CB_CORPORATION       ,cb_corporation                 );
        event.setValue(CB_PROTAL_OPUN_COD   ,cb_protal_opun_cod             );
        event.setValue(CB_PA_OPUN_COD       ,cb_pa_opun_cod                 );
        event.setValue(CB_CONNTR_NO         ,cb_conntr_no                   );
        event.setValue(CB_CONNTR_NAME       ,cb_conntr_name                 );
        event.setValue(CB_CUST_NO           ,cb_cust_no                     );
        event.setValue(CB_ACT_CUST_NAME     ,cb_act_cust_name               );
        event.setValue(CB_PC_SIGN           ,cb_pc_sign                     );
        event.setValue(CB_ACCT_NO           ,cb_acct_no                     );
        event.setValue(CB_OUT_ACCT_NO       ,cb_out_acct_no                 );
        event.setValue(CB_CARD_NO           ,cb_card_no                     );
        event.setValue(CB_VIP_ACCT_FLG      ,cb_vip_acct_flg                );
        event.setValue(CB_PUB_DEP_FLG       ,cb_pub_dep_flg                 );
        event.setValue(CB_OPAC_TYP          ,cb_opac_typ                    );
        event.setValue(CB_OPAC_TYP_CN       ,cb_opac_typ_cn                 );
        event.setValue(CB_OPAC_AMT          ,cb_opac_amt                    );
        event.setValue(CB_OPAC_INSTN_NO     ,cb_opac_instn_no               );
        event.setValue(CB_OPAC_TLR_NO       ,cb_opac_tlr_no                 );
        event.setValue(CB_OPAC_CHANNEL_FLAG ,cb_opac_channel_flag           );
        event.setValue(CB_OPAC_DT           ,cb_opac_dt                     );
        event.setValue(CB_CACCT_INSTN_NO    ,cb_cacct_instn_no              );
        event.setValue(CB_CACCT_DT          ,cb_cacct_dt                    );
        event.setValue(CB_PDP_CODE          ,cb_pdp_code                    );
        event.setValue(CB_EVENT_DATE        ,cb_event_date                  );
        event.setValue(CB_EVENT_HOUR        ,cb_event_hour                  );
        event.setValue(CB_EVENT_MINU        ,cb_event_minu                  );
        event.setValue(CB_EVENT_TIME        ,cb_event_time                  );
        event.setValue(CB_PROC_TMS          ,cb_proc_time                   );
        event.setValue(CB_PROC_TIME         , SpecialDateFormatUtil.toYmdHms(cb_proc_time));

        return event;
    }
}
